package com.finance.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.finance.common.constant.UserConstants;
import com.finance.common.core.domain.Ztree;
import com.finance.common.core.domain.entity.SysDept;
import com.finance.common.core.domain.entity.SysRole;
import com.finance.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.finance.common.annotation.Log;
import com.finance.common.enums.BusinessType;
import com.finance.web.domain.FmAccount;
import com.finance.web.service.IFmAccountService;
import com.finance.common.core.controller.BaseController;
import com.finance.common.core.domain.AjaxResult;
import com.finance.common.utils.poi.ExcelUtil;
import com.finance.common.core.page.TableDataInfo;

/**
 * 账户Controller
 *
 * @author lisen
 * @date 2021-11-11
 */
@Controller
@RequestMapping("/admin/account")
public class FmAccountController extends BaseController
{
    private String prefix = "admin/account";

    @Autowired
    private IFmAccountService fmAccountService;

    @RequiresPermissions("admin:account:view")
    @GetMapping()
    public String account()
    {
        return prefix + "/account";
    }

    /**
     * 查询账户列表
     */
    @RequiresPermissions("admin:account:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FmAccount fmAccount)
    {
        startPage();
        List<FmAccount> list = fmAccountService.selectFmAccountList(fmAccount);
        return getDataTable(list);
    }

    /**
     * 导出账户列表
     */
    @RequiresPermissions("admin:account:export")
    @Log(title = "账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FmAccount fmAccount)
    {
        List<FmAccount> list = fmAccountService.selectFmAccountList(fmAccount);
        ExcelUtil<FmAccount> util = new ExcelUtil<FmAccount>(FmAccount.class);
        return util.exportExcel(list, "账户数据");
    }

    /**
     * 新增账户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存账户
     */
    @RequiresPermissions("admin:account:add")
    @Log(title = "账户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FmAccount fmAccount)
    {
        fmAccount.setUserId(getSysUser().getUserId());
        fmAccount.setUserName(getSysUser().getUserName());
        return toAjax(fmAccountService.insertFmAccount(fmAccount));
    }

    /**
     * 修改账户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FmAccount fmAccount = fmAccountService.selectFmAccountById(id);
        mmap.put("fmAccount", fmAccount);
        return prefix + "/edit";
    }

    /**
     * 修改保存账户
     */
    @RequiresPermissions("admin:account:edit")
    @Log(title = "账户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FmAccount fmAccount)
    {
        return toAjax(fmAccountService.updateFmAccount(fmAccount));
    }

    /**
     * 删除账户
     */
    @RequiresPermissions("admin:account:remove")
    @Log(title = "账户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fmAccountService.deleteFmAccountByIds(ids));
    }

    /**
     * 选择部门树
     *
     */
    @GetMapping(value = { "/selectAccountTree"})
    public String selectAccountTree( ModelMap mmap)
    {
        return prefix + "/tree";
    }

    /**
     * 加载角色部门（数据权限）列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(FmAccount fmAccount)
    {
        List<FmAccount> accountList = fmAccountService.selectFmAccountList(null);
        List<Ztree> ztrees = new ArrayList<Ztree>();

        boolean isCheck = StringUtils.isNotNull(accountList);
        for (FmAccount account : accountList)
        {
                Ztree ztree = new Ztree();
                ztree.setId(account.getId());
                ztree.setpId(-1L);
                ztree.setName(account.getName());
                ztree.setTitle(account.getName());
                ztrees.add(ztree);
        }
        return ztrees;
    }

}
